Skip to content

fix(telemetry): keep throttled telemetry batches instead of dropping them - #461

Open
vuanhphung wants to merge 7 commits into
mainfrom
perf/rd-telemetry-throttle
Open

vuanhphung wants to merge 7 commits into
mainfrom
perf/rd-telemetry-throttle

Conversation

@vuanhphung

@vuanhphung vuanhphung commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

/telemetry-ext throttles bursts with HTTP 429 and Retry-After: 1. Telemetry export retries are retry_delay (100 ms) apart, so all three retries land inside the throttle window and the batch is dropped. With telemetry on, one Python process at ~650 QPS against a Reyden warehouse lost 9–25% of its events this way (9–22 dropped batches per minute across 6 runs), and every retry logged a WARN to the application's stderr (100–180 lines per minute).

A background export that ends in 429/503 now puts its batch back on the queue and pauses background flushes for 1 s, doubling per consecutive throttled export up to 30 s. The queue holds at most 10,000 events (oldest dropped first, counted and logged at debug). While paused, events keep queueing; afterwards the backlog goes out in posts of at most max(batch_size, 1,000) events. Flushes on session close skip the pause and use today's retry settings, so close never waits out a backoff. While the client is still in use (a host's last session closing), a throttled batch is kept, and if a backoff is already running the flush leaves the queue to the background flush instead of posting into it. A batch whose export times out is dropped as before, since the server may already have it. The final close stops at its first failed post and logs what it leaves. Telemetry retries now log at debug, matching the rest of the telemetry export failures.

Alternatives considered

  • Honor Retry-After in the HTTP retry loop. This was my first prototype. It also fixed the loss, but the final flush on close then waited out Retry-After, and the last close() took 2.6–4.6 s instead of 0.1–1.0 s while throttled.
  • Larger default batch. With the 1.4.0 wheel, telemetry_batch_size=500 avoided drops in 2 of 2 runs (versus 9–12% lost at 100), because there were fewer posts. It only lowers the request rate, though, and the limit is shared by every process using the workspace, so this PR keeps the default and handles throttling instead. A batch-size change can still be made separately.

Verification

  • cargo test --lib (1107 passed) and cargo clippy --all-targets -- -D warnings. New tests cover re-queue and retry after the backoff, the final close ignoring the backoff and clearing what it leaves, session-release flushes (re-queue on 429; no post during a running backoff), a timed-out export not being re-queued, a throttled export that finishes after close, bounded posts for a backlog, the cap on enqueue and on re-queue, backoff doubling and cap, and 429/503 classification, both directly and through the real HTTP retry loop (wiremock).
  • Mock /telemetry-ext that accepts 2 posts/s and answers 429 + Retry-After: 1 otherwise, driven through the Python connector (200 sessions × 30 s, ~950 QPS, 7 rounds, both orders): events lost 9–18% on main vs 0.0–0.3% with this PR. The 0.3% was the last batch, throttled again on the final close, which doesn't wait. Rejected requests 160–203 vs 72–80; slowest close() 332–913 ms vs 44–884 ms; QPS medians ~959 on both.
  • Live, final head, same setup as the next table, 1 round: main dropped 17 batches (~20% of events) with 137 retries. This PR dropped 0 (10 throttled batches re-queued and delivered) with 72 retries. Slowest close() 674 vs 668 ms; QPS 649 vs 656.
  • Live telemetry delivery (before the review follow-up commits) against a Reyden warehouse, Python connector with enable_telemetry=True, 200 sessions × 60 s, batch 100, 2 interleaved rounds (counted from the kernel's own HTTP and telemetry debug records):
main this PR
dropped batches 13 / 10 (~13–16% of events) 0 / 0
throttled batches re-queued, then delivered — 12 / 8
telemetry HTTP retries 135 / 129 59 / 93
slowest close() 187 / 693 ms 731 / 754 ms
  • Throughput, same setup with telemetry on and no debug logging (c800.py, 1 process × 200 sessions × 60 s, interleaved, n=3, median [min–max]):
main this PR
avg QPS 692 [669–699] 685 [621–692]
peak QPS 960 [958–992] 972 [890–998]
CPU per query 1.401 ms 1.395 ms
WARN lines on stderr per run 117–131 0
slowest close() 884 ms [327–1170] 513 ms [95–1256]
peak RSS 357 MB 380 MB

QPS differences are within the shared warehouse's run-to-run noise. Peak RSS is higher because throttled events are kept instead of dropped.

Live benchmark (staging Large Reyden warehouse)

TPC-H Q6 on a Large Real-Time warehouse, 800 sessions (4 processes x 200 threads), cold start, each session querying back to back for 120 s, 16 client cores. Telemetry is on in this workspace (enableTelemetryForKernel) and /telemetry-ext throttles. Isolated A/B vs connector main 0c1b9e80 + kernel main db23442, measured at 7fcd452: one discarded warm-up, then 4 rounds with the arm order rotated each round, nothing else on the warehouse. Median (min-max):

kernel main this PR kernel main, telemetry off
Average QPS (setup included) 2218 (2145-2260) 2225 (2122-2284) 2246 (2147-2265)
Steady QPS (after all sessions open) 2257 (2182-2302) 2265 (2165-2328) 2286 (2187-2306)
Query latency p99 (ms) 859 (823-868) 822 (793-879) 834 (807-849)
Client CPU per query (ms) 1.23 (1.19-1.35) 1.21 (1.17-1.32) 1.19 (1.15-1.24)
Peak RSS per process (MB) 389 (382-408) 423 (421-424) 369 (364-370)
Last session close after last query (s) 2.1 (1.6-2.7) 3.5 (0.4-5.1) 0.07

Throughput is unchanged: per-round average QPS vs kernel main was -0.5, -1.4, +3.7, -1.1%. Even turning telemetry off moves it only +0.8%, so telemetry is not a throughput bottleneck here. An earlier fixed-order run of this A/B showed ~+8%, but its kernel main runs were among the slowest of that night (1.8-2.1k vs mostly 2.0-2.2k in the other A/Bs); this rotated rerun replaces it. Kernel main logs 571-675 TelemetryExport 429 retry WARNs per run; this PR logs those retries at debug, so they no longer show (they are reduced, not gone). Costs: +34 MB peak RSS per process and a final close that reaches the 5 s flush timeout in 2 of 4 rounds.

SELECT 1 on an X-Large Real-Time warehouse (driver-bound). Q6 above is limited by the warehouse, so the same harness also ran SELECT 1, where the client is the limit: X-Large warehouse, 4 processes x 200 threads, 120 s, 16 client cores, this change vs connector main 0c1b9e80 + kernel main db23442, measured at 7fcd452, run in rotation with the other factory PRs, 2 rounds. Median (min-max):

kernel main this PR
Average QPS (setup included) 6158 (6135-6181) 6099 (6058-6140)
Peak QPS (best second) 6572 (6555-6590) 6524 (6504-6544)
Query latency p50 (ms) 123 (123-123) 124 (123-124)
Client CPU per query (ms) 1.33 (1.32-1.34) 1.32 (1.31-1.34)

Average QPS change per round: -1.2%, -0.7%.

This pull request and its description were written by Isaac.

…them

/telemetry-ext answers bursts with 429 (Retry-After: 1). Telemetry
retries are 100 ms apart, so all three land inside the throttle window
and the batch is dropped. With telemetry on, one process at ~650 QPS
lost 9-20% of its events this way, and every retry logged a WARN to the
application's stderr.

A background export that ends in 429/503 now puts its batch back on the
queue (bounded at 10,000 events, oldest dropped first) and pauses
background flushes for 1 s, doubling per consecutive throttled export up
to 30 s. The close-time flush ignores the pause and makes one export
attempt as before, so close is never slower than today. Telemetry
retries log at debug, like the other telemetry export failures.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 2 Medium · 1 Nit

The approach works, with two medium concerns. Session-release flushes go through the same no-requeue path as the final close, so a throttled response there can drop the whole 10k-event backlog. After a backoff, the backlog goes out as one unbounded POST.

Other findings

  • ⚪ Nit — The new tests cover the final close(). Nothing covers the path from F1: flush_bounded / schedule_flush_bounded running while the client stays active (after the last session is released but before the lease drops) and the export is throttled. If you decide that path should re-queue, a test like close_flush_ignores_backoff_and_does_not_requeue that calls flush_bounded() without close() would lock that in.

Comment thread src/telemetry/client.rs Resolved
Comment thread src/telemetry/client.rs Resolved
…ts per post

Review follow-up. flush_bounded also runs when a host's last session
closes while the client stays in use, and that path dropped a throttled
backlog. It now re-queues on 429/503 like background flushes; only the
final close (client closing) still drops. Each export now sends at most
max(batch_size, 1000) of the oldest events, and flushes keep going while
a backlog remains, so a backlog left by throttling is not sent as one
multi-megabyte request.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low

Looks good. The re-queue, backoff and cap logic is correct, and queue_count is updated consistently under the pending lock. One low-severity concern: an ordering race between back_off and requeue can make a new test flaky. Nit: the changelog says "at most 1,000 events per request", but the code sends up to max(batch_size, 1000).

Comment thread src/telemetry/client.rs
tracing::debug!(
dropped,
"telemetry backlog full while throttled; dropped oldest events"
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Low — export_once calls back_off() before requeue(batch), so throttled() turns true a moment before the batch is back in pending. The tests use wait_until(|| client.throttled()) to mean "the throttled batch has been re-queued", but that isn't guaranteed. The telemetry runtime is a separate multi-thread runtime, so the background task really does run in parallel with the test.

In close_flush_ignores_backoff_and_does_not_requeue, if the test thread sees throttled() inside that window and runs client.enqueue("two") and then close() (which sets STATE_CLOSING) before the background task gets the pending lock in requeue, then requeue sees a non-active state and drops "one". The close flush then exports only ["two"], and the assertion [["one"], ["one","two"]] fails. The window is small, but this can make the test flaky in CI.

Calling requeue(batch) before back_off() closes it: anyone who sees the throttle then also sees the re-queued events. Nothing in production depends on the current order.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in edf2bbe. export_once now calls requeue(batch) before back_off(), so anyone who sees throttled() also sees the re-queued events. The telemetry::client tests passed 8 runs in a row. The changelog Nit is fixed in the same commit: it now says at most 1,000 events per request, or one batch if larger.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't see the fix in the checked-out head. In export_once, line 232 still calls self.back_off() and line 233 then calls self.requeue(batch), so throttled() can still turn true before the batch is back in pending. Commit edf2bbe may not have been pushed, or the head I checked out may be older than it. If you push a version that calls requeue(batch) before back_off(), this race goes away. Eight clean runs in a row don't rule it out, because the window is narrow.

Review follow-up. Setting the backoff before the batch was back in the
queue let an observer of throttled() run ahead of the re-queue, which
could make the close tests flaky. Also say in the changelog that a post
holds max(batch_size, 1000) events.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 3 Low

Looks good: the re-queue, backoff and bounded-backlog logic is sound, and the state checks done under the queue lock handle the close race correctly. I found three low-severity issues: session-release flushes double the backoff, events dropped at the cap can go unlogged on close, and no wiremock test covers 429 classification through the real HTTP retry path.

Comment thread src/telemetry/client.rs Outdated
let _guard = self.flush_lock.lock().await;
self.export_pending().await;
let requeue_if_throttled = self.state.load(Ordering::Acquire) == STATE_ACTIVE;
while self.export_once(requeue_if_throttled).await {}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Low — flush_once_wait skips the throttle window, but a throttled result here still goes through export_once(true), which calls back_off() and doubles the shared backoff. A host whose last session closes several times during a throttle (open → query → close loops) sends one extra POST into the throttle window on each close, plus the HTTP layer's retries, and each one doubles the background pause, so it reaches the 30 s cap quickly. The longer pause makes it more likely that enqueue hits MAX_PENDING_EVENTS and drops the oldest events. The new test session_release_flush_keeps_throttled_batch asserts this doubling. Consider skipping the export (or the back_off) for a session-release flush while throttled() is already true. The batch is kept in the queue either way.

(Anchored to the nearest changed line — see the description for the exact location.)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d552b29. While the client is active and throttled(), flush_once_wait now returns without posting. The queue stays for the background flush after the backoff, so open/close loops no longer push extra POSTs into the throttle window or extend the backoff. When the client is not throttled, a session-release flush that gets a 429 still re-queues, and the final close still makes its one attempt. Tests: session_release_flush_keeps_throttled_batch (no post, backoff unchanged) and session_release_flush_requeues_when_it_gets_throttled.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The checked-out head doesn't contain the change described for d552b29 yet. flush_once_wait still goes straight into export_once(requeue_if_throttled) and never checks throttled(). session_release_flush_keeps_throttled_batch still expects a second POST ([["one"], ["one"]]) and a doubled backoff (INITIAL_THROTTLE_BACKOFF * 2), and session_release_flush_requeues_when_it_gets_throttled isn't in the file. The fix you describe sounds right: return early while active and throttled, and still requeue when a session-release flush gets a fresh 429. My guess is that d552b29 hasn't reached the PR branch yet. Once it's pushed, the change will be re-checked.

Comment thread src/telemetry/client.rs
}

export_batch(Arc::clone(&self.exporter), batch).await;
let result = export_batch(Arc::clone(&self.exporter), &batch).await;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Low — The dropped counter is only swapped and logged after an export_batch await finishes. If close()'s bounded flush times out mid-export (the future is cancelled before the swap) or nothing gets exported, events dropped at the queue cap are never reported. close() logs abandoned but never reads dropped. Consider also swapping dropped in close() next to the abandoned log, so the debug trail covers every dropped event.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d552b29. close() now also swaps dropped and logs it with abandoned in the same debug line, so events dropped at the cap are reported even when no export finishes.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the checked-out head and the fix isn't there. close() still only drains pending and logs abandoned, and the only dropped.swap(0, …) is the one in export_once after the export_batch await. A grep for dropped turns up no read in close(). Maybe d552b29 hasn't been pushed to this branch yet, or it's on a different branch. Once the swap and log in close() appear on the PR head, this can be resolved.

}
}

fn export_error(error: KernelError) -> TelemetryExportError {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Low — export_error is only unit-tested with errors built by KernelError::from_http_status. No test checks the real path: DatabricksHttpClient::execute(.., TelemetryExport) hits its retry limit on a 429 or 503 and returns an Error with http_status still set, which this mapping needs in order to return Throttled. If the retry loop ever wraps or replaces the error (for example, on a retry-budget timeout), batches would quietly go back to being dropped. A small wiremock test (429 + Retry-After → TelemetryExportError::Throttled) would cover this.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in d552b29: rate_limited_export_through_retry_loop_is_throttled in telemetry/databricks.rs. It drives DatabricksTelemetryExporter through a real DatabricksHttpClient against wiremock. 429 and 503 (with Retry-After) classify as Throttled both when retries are exhausted and when the retry budget is exceeded before a retry (the overall_timeout path). A 500 stays Export.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for writing this up. I can't find it in the checked-out head, though. src/telemetry/databricks.rs has no rate_limited_export_through_retry_loop_is_throttled, and its test module still has only three tests: endpoint_url_normalizes_host_and_endpoint, rate_limit_statuses_classify_as_throttled (which still builds errors with KernelError::from_http_status) and build_export_request_sets_method_url_json_body_and_content_type. Searching the repo for the test name also finds nothing. Most likely d552b29 hasn't been pushed to this branch yet, or it went to a different branch. Once it's on the PR head, the test you describe would address the concern: 429/503 through the real DatabricksHttpClient classify as Throttled, both when retries run out and on the overall_timeout path, and 500 stays Export. Until then, I'll keep this thread open.

Review follow-up. While the client is still in use and throttled, the
flush run when a host's last session closes now leaves the queue to the
background flush instead of posting into the throttle window and
doubling the backoff on every session close. close() also reports
events dropped at the queue cap. A wiremock test checks that a 429/503
coming out of the HTTP retry loop (retries exhausted, or retry budget
exceeded) classifies as Throttled.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 2 Low

Looks good overall. The re-queue/backoff design is consistent across the background, session-release and close paths, and it has good unit and wiremock coverage. I found one low-severity inefficiency (task spawns while a throttle backlog drains) and two doc comments that no longer match the code.

Comment thread src/telemetry/client.rs Resolved
Comment thread src/telemetry/client.rs Resolved
Review follow-up. While a throttle backlog drains, the queue stays at or
above batch_size, so every enqueue spawned a flush task that only failed
to take the flush lock. A flag set for the length of a background flush
now skips those spawns; the running flush re-checks the queue before it
exits. Also correct two doc comments (the closing flush stops at its
first failed post; the cap is mainly reached while throttled).

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No issues identified by the review bot.

This branch was previously deployed

1 inactive deployment
azure-prod — 7fcd4522 Deployed Nov 13, 2026 by oss-reviewer-bot[bot] via followup #2932
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant